package cn.sdormitory.controller.smartdor;


import cn.sdormitory.basedata.entity.BClass;
import cn.sdormitory.basedata.service.BClassService;
import cn.sdormitory.basedata.vo.BClassDorAndHyVo;
import cn.sdormitory.common.annotation.IgnoreAuth;
import cn.sdormitory.common.api.CommonPage;
import cn.sdormitory.common.api.CommonResult;
import cn.sdormitory.common.utils.PropertiesUtils;
import cn.sdormitory.request.HttpRequest;
import cn.sdormitory.sys.entity.SysUser;
import cn.sdormitory.sys.service.SysUserService;
import cn.sdormitory.sys.vo.LoginInfoVo;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.security.Principal;
import java.util.List;
import java.util.Map;

/**
 * @创建人：zhouyang
 * @创建时间：2020/12/4 15:33
 * @version：V1.0
 */
@RestController
@Api(tags = "Smartdor-aidevice=> 首页")
@RequestMapping("/smartdor/aidevice")
public class IndexController {
    @Resource
    private SysUserService sysUserService;
    @Resource
    private BClassService bClassService;

    @ApiOperation("aiopen => 首页开闸")
    @GetMapping(value = "/aiopen")
    public CommonResult<Integer> aiopen() {
        String ip = PropertiesUtils.get("device.properties","sdormitory.device1.ip");
        String key = PropertiesUtils.get("device.properties","sdormitory.device1.key");
        String url=ip+"/open";
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append("key="+key);
        String result= HttpRequest.sendGet(url,stringBuffer.toString());
        JSONObject parseObject=JSONObject.parseObject(result);
        String status=parseObject.getString("status");
        int count=0;
        if (""!=status && status!=null && status.equals("0")) {
            count=1;
            return CommonResult.success(count);
        }else{
            return CommonResult.failed();
        }
    }

    @IgnoreAuth
    @ApiOperation("getLoginRoleNames =>获取当前登录人拥有的角色")
    @GetMapping(value = "/getLoginRoleNames")
    public CommonResult<String> getLoginRoleNames(Principal principal) {
        String roleNames=sysUserService.getLoginRoleNames(principal.getName());
        if (""!=roleNames && roleNames!=null) {
            return CommonResult.success(roleNames);
        }else{
            return CommonResult.failed("当前用户无任何角色!!!");
        }
    }

//    @IgnoreAuth
//    @ApiOperation("listHygieneBadDor => 获取当前班主任拥有的班级列表")
//    @GetMapping(value = "/listClassByTeaName")
//    public CommonResult<List<BClass>> listClassByTeaName(Principal principal)  {
//        SysUser sysUser=sysUserService.getByUserName(principal.getName());
//        List<BClass> bClassList=bClassService.listClassByTeacher(sysUser.getId());
//        return CommonResult.success(bClassList);
//    }

    @IgnoreAuth
    @ApiOperation("listHygieneBadDor => 获取当前班主任拥有的班级列表及对应的宿舍和卫生信息")
    @GetMapping(value = "/listClassByTeaName")
    public CommonResult<List<BClassDorAndHyVo>> listClassByTeaName(Principal principal,@RequestParam Map<String,String> map)  {
        SysUser sysUser=sysUserService.getByUserName(principal.getName());
        List<BClassDorAndHyVo> bClassDorAndHyVos=bClassService.getClassDorAndHyListByTeaId(sysUser.getId(),map.get("checkDate"));
        return CommonResult.success(bClassDorAndHyVos);
    }


}
